<?php
/**
 * Created by YuanXiShop.
 * User: YuanXi
 * Date: 2021/6/3 0003
 * Time: 1:26
 */

namespace admin;

use JasonGrimes\Paginator;
use models\BaseDao;
use Slince\Upload\UploadHandlerBuilder;

class User extends Admin
{
    function __construct()
    {
        parent::__construct();
        $this->assign('menumark','user');
    }
    function index()
    {
        $db = new BaseDao();

        $num = $_GET['num'] ?? 1;
        //搜索条件
        $prosql['ORDER'] = ['id'=>'DESC'];

        $where = [];

        if(!empty($_GET['orderby'])){
            $prosql['ORDER'] = $_GET['orderby'];
            $orderby = '&orderby='.$_GET['orderby'];
        }
        if(!empty($_GET['name'])){
            $where['name[~]'] = $_GET['name'];
            $name = '&name='.$_GET['name'];
        }
        if(!empty($_GET['phone'])){
            $where['phone[~]'] = $_GET['phone'];
            $phone = '&phone='.$_GET['phone'];
        }
        if(!empty($_GET['email'])){
            $where['email[~]'] = $_GET['email'];
            $email = '&email='.$_GET['email'];
        }
        $this->assign('get',$_GET);

        $totalItems = $db->count('user',$where);//总页面数
        $itemsPerPage = 3;//每页显示多少条数据
        $currentPage = $num;//当前页是第几页
        $urlPattern = '/admin/user?num=(:num)'.$name.$phone.$email.$orderby;

        $paginator = new Paginator($totalItems, $itemsPerPage, $currentPage, $urlPattern);

        //从第几页开始
        $start = ($currentPage-1) * $itemsPerPage;
        $prosql['LIMIT'] = [$start,$itemsPerPage];

        $prosql = array_merge($prosql,$where);
        $data = $db->select('user','*',$prosql);

        //将数据分配给模板
        $this->assign('data',$data);
        $this->assign('fpage',$paginator);

        //标题
        $this->assign('title','用户列表');
        //输出模板
        $this->display('user/index');
    }
    function mod($id)
    {
        $db = new BaseDao();
        $data = $db->get('user','*',['id'=>$id]);
        $this->assign($data);
        $this->assign('title','修改用户');
        $this->display('user/mod');
    }
    function doupdate()
    {
        $id = $_POST['id'];
        unset($_POST['id']);
        $db = new BaseDao();
        if(!empty($_POST['pw'])){
            $_POST['pw'] =  md5(md5('ew_'.$_POST['pw']));
        }else{
            unset($_POST['pw']);
        }

        if($db->update('user',$_POST,['id'=>$id])){
            $this->success('/admin/user','修改成功！');
        }else{
            $this->error('/admin/user/mod/'.$id,'修改失败！');
        }
    }
    function del($id)
    {
        $db = new BaseDao();
        if($db->delete('user',['id'=>$id])){
            $this->success('/admin/user','删除成功！');
        }else{
            $this->error('/admin/user','删除失败！');
        }
    }
    function alldel()
    {
        $db = new BaseDao();
        $num = 0;
        foreach ($_POST['id'] as $id){
            $num += $db->delete('user',['id'=>$id]);
        }
        if($num > 0){
            $this->success('/admin/user',$num.'个用户删除成功！');
        }else{
            $this->error('/admin/user','删除失败！');
        }
    }

}